#Script to replicate Figure 7 in the Online Appendix

#Figure 7: Level of specificity of UPR recommendations submitted by China and other
#UN members (N=89,851)


#load libraries and dataframes
pacman::p_load(MASS,sjPlot,dplyr,countrycode,readxl,ggeffects, tidyverse)

upr <- readRDS("upr_data.rds")
load("AgreementScores.Rdata")
controls <- read.csv("controls.csv")
trade <- readRDS("trade.rds")

### UNGA voting

dfAgree <- dfAgree[,c("ccode1","ccode2","year","IdealPointDistance")]
names(dfAgree) <- c("rs_ccode","sur_ccode","year","un_agreement")
upr <- merge(upr,dfAgree,by=c("rs_ccode","sur_ccode","year"),all.x=TRUE)

### Trade

trade$ccode_sender <- countrycode(trade$country1_iso,"iso3c","cown",custom_match=c('SRB'=345))
trade$ccode_target <- countrycode(trade$country2_iso,"iso3c","cown",custom_match=c('SRB'=345))
trade$trade_total <- apply(X=trade[,c("imp_country1","imp_country2","exp_country1","exp_country2")], 
                           MARGIN=1, FUN=mean, na.rm=TRUE)
trade$trade_total <- ifelse(is.nan(trade$trade_total),NA,trade$trade_total)
trade <- trade[,c("year","ccode_sender","ccode_target","trade_total")]
names(trade) <- c("year","rs_ccode","sur_ccode","trade_total")
upr <- merge(upr,trade,by=c("rs_ccode","sur_ccode","year"),all.x=TRUE)
upr$trade_total <- ifelse(is.na(upr$trade_total),0,upr$trade_total)

### Alliance

alliance <- read.csv("alliance.csv")
df_all_aux <- upr[upr$year>2012,]
df_all_aux <- df_all_aux[,c("rs_ccode","sur_ccode","year")]
alliance_aux <- alliance[alliance$dyad_end_year==2012,]
alliance_aux <- alliance_aux[,c("ccode1","ccode2","defense","neutrality",
                                "nonaggression","entente")]
alliance_aux <- unique(alliance_aux)
alliance_aux <- alliance_aux %>% group_by(ccode1,ccode2) %>%
  summarise(defense=max(defense,na.rm=TRUE))
df_all_aux <- merge(alliance_aux,df_all_aux[,c("rs_ccode","sur_ccode","year")],by.x=c("ccode1","ccode2"),
                    by.y=c("rs_ccode","sur_ccode"),all.x=TRUE)
alliance <- alliance[,c("ccode1","ccode2","year","defense")]
alliance <- rbind(alliance,df_all_aux)
alliance <- alliance %>% group_by(ccode1,ccode2,year) %>%
  summarise(defense=max(defense,na.rm=TRUE))
upr <- merge(upr,alliance,by.x=c("rs_ccode","sur_ccode","year"),
                by.y=c("ccode1","ccode2","year"),all.x=TRUE)
upr$defense <- ifelse(is.na(upr$defense),0,upr$defense)

### World Bank

WorldDatabank <- read_excel("WorldDatabank.xlsx")
names(WorldDatabank) <- c("country","stateabb","year","year_f","gdp","gdp_growth",
                          "gdp_pc","gdp_pc_growth","population")
WorldDatabank <- WorldDatabank[,c(1,3,5:length(WorldDatabank))]
WorldDatabank$ccode_sender <- countrycode(WorldDatabank$country,"country.name","cown",
                                          custom_match=c('Serbia'=345))
WorldDatabank$gdp_pc <- as.numeric(WorldDatabank$gdp_pc)
WorldDatabank$gdp <- as.numeric(WorldDatabank$gdp)
WorldDatabank$gdp_growth <- as.numeric(WorldDatabank$gdp_growth)
WorldDatabank$gdp_pc_growth <- as.numeric(WorldDatabank$gdp_pc_growth)
WorldDatabank$population <- as.numeric(WorldDatabank$population)
WorldDatabank$gdp_pc[is.na(WorldDatabank$gdp_pc)] <- WorldDatabank$gdp[is.na(WorldDatabank$gdp_pc)]/WorldDatabank$population[is.na(WorldDatabank$gdp_pc)]
WorldDatabank <- WorldDatabank[,c("ccode_sender","year","gdp","gdp_pc","population")]
names(WorldDatabank) <- c("rs_ccode","year","rs_gdp","rs_gdp_pc","rs_population")

upr <- upr[!is.na(upr$rs_ccode),]
upr <- upr[!is.na(upr$sur_ccode),]
upr <- merge(upr,WorldDatabank,by=c("rs_ccode","year"),all.x=TRUE)
names(WorldDatabank) <- c("sur_ccode","year","sur_gdp","sur_gdp_pc","sur_population")
upr <- merge(upr,WorldDatabank,by=c("sur_ccode","year"),all.x=TRUE)
rm(WorldDatabank,alliance,alliance_aux,dfAgree,trade,df_all_aux)

upr$rs_gdp <- ifelse(is.na(upr$rs_gdp),mean(na.omit(upr$rs_gdp)),upr$rs_gdp)
upr$sur_gdp <- ifelse(is.na(upr$sur_gdp),mean(na.omit(upr$sur_gdp)),upr$sur_gdp)

upr$china <- ifelse(upr$rs_ccode==710,"China","Any Other Country")
unique_sur <- unique(upr[,c("sur_ccode","year")])
unique_sur_code <- paste0(unique_sur$sur_ccode,"_",unique_sur$year)
upr$Action.Category <- factor(upr$Action.Category,ordered=TRUE,
                              levels=c("1 - Minimal action","2 - Continuing action",
                                       "3 - Considering action","4 - General action","5 - Specific action"))
ologit1 <- polr(Action.Category ~ china + physint + other + migrants + justice + cp.rights + ind.rights + 
                  minorities + socio.econ + instruments + log(trade_total+1) + defense + 
                  log(rs_gdp) + log(sur_gdp) + as.factor(year) + as.factor(sur_ccode), 
                data = upr, Hess=TRUE)

x <- plot_model(ologit1, type = "pred", terms = "china", dodge = 0.75, ci.lvl=0.95,spacing=0.5) +
  xlab("Type of Recommendation") + ylab("Pr[Make Recommendation]") + ggtitle("") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(),
        panel.border = element_rect(colour = "black", fill=NA))
x$facet$params$nrow=1

png("figure_7.png",2700,1350,res=275)
x
dev.off()